package com.ruoyi.web.dorm.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.dorm.entity.StudentEntity;
import com.ruoyi.web.dorm.entity.dto.StudentDTO;
import com.ruoyi.web.dorm.entity.vo.StudentVO;
import com.ruoyi.web.dorm.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/dorm/student")
@Api(tags = "学生管理")
public class StudentController extends BaseController {

    @Autowired
    private StudentService studentService;


    /**
     * 查询学生信息
     * @param studentDTO
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/studentList")
    public TableDataInfo studentList(StudentDTO studentDTO){
        startPage();
        return getDataTable(studentService.studentList(studentDTO));
    }

    /**
     * 添加学生信息
     * @param studentEntity
     * @return
     */
    @PostMapping("/addStudent")
    public AjaxResult addStudent(@RequestBody StudentEntity studentEntity,StudentDTO studentDTO){
        return toAjax(studentService.addStudent(studentEntity,studentDTO));
    }

    /**
     * 更新学生信息
     * @param studentEntity
     * @return
     */
    @PostMapping("/updateStudent")
    public AjaxResult updateStudent(@RequestBody StudentEntity studentEntity,StudentDTO studentDTO){
        return toAjax(studentService.updateStudent(studentEntity,studentDTO));
    }

    /**
     * 删除学生信息
     * @param id
     * @return
     */
    @DeleteMapping("/delStudent")
    public AjaxResult delStudent(Long id){
        return toAjax(studentService.delStudent(id));
    }


    /**
     * 查询学生信息导出信息
     * @param studentDTO
     * @return
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,StudentDTO studentDTO){
       List<StudentVO> list = studentService.studentList(studentDTO);
        ExcelUtil<StudentVO> util = new ExcelUtil<>(StudentVO.class);
        util.exportExcel(response,list,"学生信息数据");
    }


}
